#include "Potion.h"
#include "Game.h"
#include "ObjectDatabase.h"
#include "StringLib.h"
#include "Renderer.h"
#include "Util.h"
#include "Level.h"
#include "Tile.h"
#include "Character.h"

Potion::Potion(PotionType type, int power) : ScrambledItem(GT_IT_POTION, TH_POTION_TYPE, g_objectDatabase->getScrambledPotionType(type)) {
	m_types[TH_POTION_TYPE] = type;
	m_power = power;
	const PotionInfo& info = g_objectDatabase->getPotionInfo(type);
	m_weight = info.weight;
	m_stackable = true;
}

Potion::Potion(std::ifstream& file) : ScrambledItem(GT_IT_POTION, file) {
	int* tokens = Util::intSplit(file);
	m_types[TH_POTION_TYPE] = tokens[0];
	const PotionInfo& info = g_objectDatabase->getPotionInfo(PotionType(tokens[0]));
	m_weight = info.weight;
	m_stackable = true;
	m_power = tokens[1];
	load(TH_POTION_TYPE, g_objectDatabase->getScrambledPotionType(PotionType(tokens[0])));
	delete[] tokens;
}

Potion::~Potion() {
}

void Potion::save(std::ofstream& file) const {
	file << getType(TH_GENERIC_TYPE) << "\n";
	ScrambledItem::save(file);
	file << getType(TH_POTION_TYPE) << " " << m_power << "\n";
}

void Potion::render(float x, float y) {
	g_renderer->render(*this, x, y);
}

void Potion::logDetails(TextLog& log) const {
	Item::logDetails(log);
	log.addBlankLine();
	if(isIdentified())
		log.addLine(g_stringLib->getTextString(ST_POTION_POWER, Util::intToString(m_power).c_str()));
}

void Potion::shatter(int x, int y) {
	g_textLog->addLine(g_stringLib->getTextString(ST_POTION_SHATTER, getName().c_str()));
	switch(getType(TH_POTION_TYPE)) {
	case PT_POISON:
		for(int i = -1; i <= 1; i++) {
			for(int j = -1; j <= 1; j++) {
				if(g_currentLevel->getTile(x+i, y+j)->getCharacter() != NULL)
					g_currentLevel->getTile(x+i, y+j)->getCharacter()->addStatusEffect(SE_POISON, 200);
			}
		}
		break;
	}
	reduceStackCount(1, true, g_currentLevel->getTile(x, y)->getItems());
}
